package base.java9_17;

import java.time.LocalDate;
import java.util.List;
import java.util.Random;

/**
 * switch改进
 *
 * @author 王霄
 * @date 2021/9/26 22:14
 */
public class SwitchDemo {
    public static void main(String[] args) {
        Random random = new Random();

        //java12
        String num12 = switch (random.nextInt(4)) {
            case 0 -> "0";
            case 1, 2, 3 -> "1 or 2 or 3";
            default -> null;
        };
        System.out.println("num12 = " + num12);


        //java14
        String num14 = switch (random.nextInt(4)) {
            case 0 -> {
                System.out.println("hahaha");
                yield "0";
            }
            case 1 -> "1";
            case 2 -> "2";
            case 3 -> "3";
            default -> null;
        };
        System.out.println("num14 = " + num14);


        //java17
        List<?> list = List.of(180, "王霄", 80.0, LocalDate.of(1996, 9, 1));
        Object o = list.get(random.nextInt(4));
//        switch (o) {
//            case Integer i -> System.out.printf("身高%dcm%n", i);
//            case Double d -> System.out.printf("体重%.2fkg%n", d);
//            case String s -> System.out.printf("姓名%s%n", s);
//            case LocalDate date -> System.out.printf("生日%s%n", date);
//            default -> System.out.println(o);
//        }
    }
}
